//
//  FeedBackVC.m
//  International_Hospital
//
//  Created by 沈宇斐的Mac on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "FeedBackVC.h"
@interface FeedBackVC ()<UITextViewDelegate>


@property (nonatomic,strong)UILabel *wordLabelCount;

@property(nonatomic,assign)int count;

@property (nonatomic,strong)UITextView *textViewLength;

@property (nonatomic,strong)UILabel *ploLabel;

@end

@implementation FeedBackVC

#pragma mark - 懒加载
- (UILabel *)wordLabelCount {
    
    if (_wordLabelCount == nil) {
        
        _wordLabelCount = [[UILabel alloc]init];
    }
    
    return _wordLabelCount;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
//    添加监听方法
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
 
    [self setupUI];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.textViewLength resignFirstResponder];
}

#pragma mark - 实现leftItem的按钮监听方法
- (void)goBack {
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 当textField布局发生改变的时候调用

- (void)textViewDidChange:(UITextView *)textView {
    
//    对占位符的显示和隐藏做判断
    if (self.textViewLength.text .length == 0) {
        
        self.ploLabel.text =  @"请输反馈信息";
        
    }else {
        
        self.ploLabel.text = @"";
    }
    
//    读出textView字符长度
     self.wordLabelCount.text = [NSString stringWithFormat:@"%lu",200 - self.textViewLength.text .length];
    
    if (self.textViewLength.text .length > 200) {
        
//        对超出的部分进行剪切
         self.textViewLength.text = [self.textViewLength.text substringToIndex:10];
             
        self.wordLabelCount.text = @"0";
    }
   
    
    if (self.textViewLength.text .length >= 200) {
        
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"亲!最多只能输入200个字!请您合理安排内容!" preferredStyle:UIAlertControllerStyleAlert];
        
               [self presentViewController:alertController animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
           
        });
       
    }
        
//    }
    
}

#pragma mark - 设置UI界面
- (void)setupUI {
    
    UITextView *textView = [[UITextView alloc]init];
    [self.view addSubview:textView];
//    textView.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    self.textViewLength = textView;
    
    textView.delegate = self;
    
//    textView.text = @"请输反馈信息";
//    textView.textColor = [UIColor grayColor];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        

        make.left.equalTo(self.view).offset(5);
        
        make.right.equalTo(self.view).offset(-5);
        
        make.top.equalTo(self.view).offset(69);
        
        make.height.mas_equalTo(150);
        
    }];
    
   
    textView.backgroundColor = [UIColor colorWithRed:241/255.0 green:239/255.0 blue:247/255.0 alpha:1];
//    进行偏置处理
    textView.contentInset = UIEdgeInsetsMake(-5, 0, 0, 0);
    
//    NSRange range;
//    range.location = 0;
//    range.length = 0;
//    textView.selectedRange = range;
    
//    [textView setFrame:CGRectMake(0,60,50 ,20)];
    
//    默认与顶部有5 的间距
//    将textView光标置顶
    
        self.automaticallyAdjustsScrollViewInsets = NO;
       
    UILabel *placeholderLabel = [[UILabel alloc]init];
    
    [self.view addSubview:placeholderLabel];
    
    self.ploLabel = placeholderLabel;
    
    placeholderLabel.font = [UIFont systemFontOfSize:15];
    
    [placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(textView.top);
        make.left.mas_equalTo(textView.left).offset(5);
        make.height.mas_equalTo(20);
        
    }];
    
    placeholderLabel.text = @"请输反馈信息";
    
    placeholderLabel.enabled = NO;
    
    placeholderLabel.textColor = [UIColor clearColor];
    
    
    UITableViewCell *feedBackCell = [[UITableViewCell alloc]init];
    
    [self.view addSubview:feedBackCell];
    
    feedBackCell.userInteractionEnabled = YES;
            
    [feedBackCell mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(textView.mas_bottom);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(40);
        
    }];
    
    UILabel *label = [[UILabel alloc]init];
    
    [feedBackCell addSubview:label];
    
    UIButton *feedBackButton = [[UIButton alloc]init];
    
    [feedBackCell addSubview:feedBackButton];
    
    UILabel *wordCountLabel = [[UILabel alloc]init];
    
    [feedBackCell addSubview:wordCountLabel];
    
    self.wordLabelCount = wordCountLabel;
    
    [wordCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(feedBackCell.mas_top);
        make.bottom.mas_equalTo(feedBackCell.mas_bottom);;
        make.width.mas_equalTo(40);
        make.right.mas_equalTo(feedBackButton.mas_left).offset(-10);

        
    }];
    
    wordCountLabel.text = @"200";
    
    wordCountLabel.font = [UIFont systemFontOfSize:17];

    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(feedBackCell.mas_top);
        make.bottom.mas_equalTo(feedBackCell.mas_bottom);;
        make.width.mas_equalTo(220);
        make.right.mas_equalTo(wordCountLabel.mas_left).offset(-10);
        
    }];
    
    [feedBackButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(feedBackCell.mas_top).offset(2);
        make.bottom.mas_equalTo(feedBackCell.mas_bottom).offset(-2);;
//        make.left.mas_equalTo(feedBackCell.mas_left).offset(80);
        make.right.mas_equalTo(feedBackCell.mas_right).offset(-5);
        make.width.mas_equalTo(90);
        
    }];
    
    label.text = @"您还可以输入的字数为:";
    
    
    label.textAlignment = NSTextAlignmentRight;
    
    label.font = [UIFont systemFontOfSize:15];
    
    [feedBackButton setTitle:@"提交反馈" forState:UIControlStateNormal];
    
    [feedBackButton setTitleColor:[UIColor colorWithRed:218/255.0 green:245/255.0 blue:246/255.0 alpha:1] forState:UIControlStateHighlighted];
    
    feedBackButton.userInteractionEnabled = YES;

    feedBackButton.backgroundColor = [UIColor colorWithRed:0 green:186/255.0 blue:202/255.0 alpha:1];
    
    
    
    UITableViewCell *WLCell = [[UITableViewCell alloc]init];
    
    [self.view addSubview:WLCell];
    
    
       WLCell.backgroundColor = [UIColor colorWithRed:241/255.0 green:239/255.0 blue:247/255.0 alpha:1];
    [WLCell mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(feedBackCell.mas_bottom);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(50);
        
    }];
  
    UILabel *WLLabel = [[UILabel alloc]init];
    
    [WLCell addSubview:WLLabel];
    
    
    [WLLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(WLCell.mas_top);
        make.bottom.mas_equalTo(WLCell.mas_bottom);;
        make.width.mas_equalTo(220);
        make.left.mas_equalTo(WLCell.mas_left).offset(10);
    }];
    
    
    WLLabel.text = @"欢迎联系我们";
    
    WLLabel.font = [UIFont systemFontOfSize:17];
    
    WLLabel.textAlignment = NSTextAlignmentLeft;
    
    
    
    
    
    UITableViewCell *severCell = [[UITableViewCell alloc]init];
    
    [self.view addSubview:severCell];
    
    [self cantectCell:severCell otherCell:WLCell string:@"用户交流群: QQ 464768569" imageString:@"qq"];
    
    
    UITableViewCell *userCell = [[UITableViewCell alloc]init];
    
    [self.view addSubview:userCell];
    
    [self cantectCell:userCell otherCell:severCell string:@"客服电话: 400-636-6113" imageString:@"dianhua"];
    
}

#pragma mark - 联系cell个封装的方法的实现
- (void)cantectCell:(UITableViewCell *)ownCell  otherCell:(UITableViewCell *)otherCell string:(NSString *)string  imageString:(NSString *)imageString{
    
    [ownCell mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(otherCell.mas_bottom);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(40);
        
    }];
    
    UILabel *userLabel = [[UILabel alloc]init];
    
    [ownCell addSubview:userLabel];
    
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(ownCell.mas_top);
        make.bottom.mas_equalTo(ownCell.mas_bottom);;
        make.width.mas_equalTo(250);
        make.left.mas_equalTo(ownCell.mas_left).offset(60);
    }];
    
    
    userLabel.text = string; //@"用户交流群: QQ 464768569";
    
    userLabel.font = [UIFont systemFontOfSize:17];
    
    userLabel.textAlignment = NSTextAlignmentLeft;
    
    UIImageView *userImage = [[UIImageView alloc]init];
    [ownCell addSubview:userImage];
    
    [userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(ownCell.mas_top).offset(10);
        make.bottom.mas_equalTo(ownCell.mas_bottom).offset(-10);
        
        make.left.mas_equalTo(ownCell.mas_left).offset(20);
        make.right.mas_equalTo(userLabel.mas_left).offset(-20);
        
    }];
    
    userImage.image = [UIImage imageNamed:imageString];
    
}

#pragma mark - 移除监听方法
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//- (void)textViewDidChangeSelection:(UITextView *)textView
//{
//    
//}

@end
